# -*- coding: utf-8 -*-
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
import os, time

def SaveImage(link, KeyWord, count):
    try:
        time.sleep(0.2)
        #urllib.request.urlretrieve(link, "imgtest/%s.jpg" % str(count))
        urllib.request.urlretrieve(link, './'+KeyWord+'/'+str(count)+'.jpg')
    except Exception :
        time.sleep(1)
        print("产生未知错误，放弃保存")
    else:
        print("图+1,已有" + str(count) + "张图")

def FindLink(PageNum, URLEncode, KeyWord):
    for i in range(PageNum):
        print('PageNum: ', i)
        try:
            #NOTE: 请针对性修改Bing图片搜索路径！！
            url = 'https://cn.bing.com/images/async?q={0}&first={1}&count=35&tsc=ImageBasicHover&layout=RowBased_Landscape&mmasync=1&dgState=x*1648_y*1763_h*185_c*6_i*71_r*10&IG=B2C9B54A468C4CCBB8EA937BF5C8921A&SFX=2&iid=images.5817'
            url = url.format(URLEncode, i*35+1)
            print(url)
            #定义请求头
            agent = {'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.165063 Safari/537.36 AppEngine-Google."}
            page1 = urllib.request.Request(url, headers=agent)
            page = urllib.request.urlopen(page1)
            #使用beautifulSoup进行解析网页
            soup = BeautifulSoup(page.read(), 'html.parser')

            #创建文件夹
            if not os.path.exists("./"+KeyWord):
                os.mkdir('./'+KeyWord)

            #方式一
            #for StepOne in soup.select('.mimg'):
            #    link = StepOne.attrs['src']
            #    count = len(os.listdir('./'+KeyWord)) + 1
            #    SaveImage(link, KeyWord, count)

            #方式二
            for StepOne in soup.select('.iusc'):
                link = StepOne.attrs['href']
                url = link
                #通过split函数从预览地址中分离出image粗地址
                #%3a%2f%2f为：//
                mainURL = url.split("%3a%2f%2f",1)
                mainURL = mainURL[1]
                otherURL = mainURL.split(".jpg", 1)
                #替换剩余字段的“%2f”为“/”
                otherURL = otherURL[0].replace("%2f", "/")
                #加上头部和尾部照片格式即可获得照片服务器地址
                imageURL = "http://"+otherURL+".jpg"
                link = imageURL
                count = len(os.listdir('./'+KeyWord)) + 1
                SaveImage(link, KeyWord, count)

        except:
            print('URL OPENING ERROR !')


if __name__=='__main__':
    PageNum = 2
    KeyWord='危险品罐车'

    URLEncode = urllib.parse.quote(KeyWord)
    print(KeyWord, ' url encode -> ', URLEncode)
    FindLink(PageNum, URLEncode, KeyWord)

